1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy;
8
9 import org.indy.util.StringList;
10
11
12 /***
13 * Description of the Class
14 *
15 *@author owen
16 */
17 public class RFCReply {
18 private int numericCode;
19 private StringList text = new StringList();
20 private String textCode;
21
22 /***
23 * Constructor for the IdRFCReply object
24 */
25 public RFCReply() {
26 }
27
28 /***
29 * Constructor for the IdRFCReply object
30 *
31 *@param reply Description of the Parameter
32 */
33 public RFCReply(RFCReply reply) {
34 }
35
36 /***
37 * Sets the numericCode attribute of the IdRFCReply object
38 *
39 *@param code The new numericCode value
40 */
41 public final void setNumericCode(int code) {
42 numericCode = code;
43
44 if (code > 0) {
45 textCode = String.valueOf(code);
46 }
47 }
48
49 /***
50 * Sets the textCode attribute of the IdRFCReply object
51 *
52 *@param s The new textCode value
53 */
54 public final void setTextCode(String s) {
55 textCode = s;
56
57 if (s.length() > 0) {
58 try {
59 numericCode = Integer.parseInt(s);
60 }
61 catch (NumberFormatException nfe) {
62 numericCode = 0;
63 }
64 }
65 }
66
67 /***
68 * Sets the reply attribute of the IdRFCReply object
69 *
70 *@param numericCode The new reply value
71 *@param data The new reply value
72 */
73 public final void setReply(int numericCode, String data) {
74 this.numericCode = numericCode;
75 this.text.setText(data);
76 }
77
78 /***
79 * Sets the text attribute of the IdRFCReply object
80 *
81 *@param text The new text value
82 */
83 public final void setText(StringList text) {
84 /***
85 *@todo Should use a copy constructor for security?
86 */
87 this.text.copy(text);
88 }
89
90 /***
91 * Gets the numericCode attribute of the IdRFCReply object
92 *
93 *@return The numericCode value
94 */
95 public final int getNumericCode() {
96 return numericCode;
97 }
98
99 /***
100 * Gets the text attribute of the IdRFCReply object
101 *
102 *@return The text value
103 */
104 public final StringList getText() {
105 return text;
106 }
107
108 /***
109 * Gets the textCode attribute of the IdRFCReply object
110 *
111 *@return The textCode value
112 */
113 public final String getTextCode() {
114 return textCode;
115 }
116
117 /***
118 * Description of the Method
119 */
120 public final void clear() {
121 textCode = null;
122 numericCode = 0;
123 text.clear();
124 }
125
126 /***
127 * Description of the Method
128 *
129 *@return Description of the Return Value
130 */
131 public final String generateReply() {
132 StringBuffer sb = new StringBuffer();
133
134 if (numericCode > 0) {
135 if (text.size() > 0) {
136 for (int i = 0, n = text.size(); i < n; i++) {
137 sb.append(numericCode);
138
139 if (i < (n - 1)) {
140 sb.append('-');
141 }
142 else {
143 sb.append(' ');
144 }
145
146 sb.append(text.get(i));
147 sb.append("\r\n");
148 }
149 }
150 else {
151 sb.append(numericCode);
152 sb.append("\r\n");
153 }
154 }
155 else if (text.size() > 0) {
156 return text.getText();
157 }
158
159 return sb.toString();
160 }
161
162 /***
163 * Description of the Method
164 *
165 *@param strings Description of the Parameter
166 */
167 public final void parseResponse(StringList strings) {
168 clear();
169
170 String textCodeTmp;
171
172 if (strings.size() > 0) {
173 //get 4 chars for POP3
174 textCodeTmp = strings.get(0).substring(0, 3).trim();
175
176 if (textCodeTmp.length() == 4) {
177 if (textCodeTmp.endsWith("-")) {
178 textCodeTmp = textCodeTmp.substring(0, 2);
179 }
180 }
181
182 setTextCode(textCodeTmp);
183 text.addStrings(strings);
184 }
185 }
186
187 //Needed for IMAP4
188
189 /***
190 * Description of the Method
191 *
192 *@param tag Description of the Parameter
193 *@param strings Description of the Parameter
194 */
195 public void parseLineResponse(String tag, StringList strings) {
196 clear();
197
198 if (strings.size() > 0) {
199 if (strings.get(strings.size() - 1).charAt(0) == '*') {
200 //untagged response
201 textCode = "OK";
202 }
203 else {
204 textCode = strings.get(strings.size() - 1)
205 .substring(tag.length() + 1, tag.length() + 4).trim();
206 }
207
208 for (int i = 0, n = strings.size(); i < n; i++) {
209 if (tag.equalsIgnoreCase(strings.get(i).substring(0, tag.length())
210 .trim())) {
211 text.add(strings.get(i).substring(tag.length() + 4));
212 }
213 else {
214 text.add(strings.get(i).substring(2));
215 }
216 }
217 }
218 }
219
220 //Needed for IMAP4
221
222 /***
223 * Description of the Method
224 *
225 *@param tag Description of the Parameter
226 *@param strings Description of the Parameter
227 */
228 public void parseResponse(String tag, StringList strings) {
229 clear();
230
231 if (strings.size() > 0) {
232 if (strings.get(strings.size() - 1).trim()
233 .equalsIgnoreCase("+ Ready for argument")) {
234 /*
235 * do not localize
236 */
237 textCode = "OK";
238 }
239 else {
240 textCode = strings.get(strings.size() - 1)
241 .substring(tag.length() + 2, tag.length() + 5).trim();
242 }
243
244 for (int i = 0, n = strings.size(); i < n; i++) {
245 if (tag.equalsIgnoreCase(strings.get(i).substring(0, tag.length())
246 .trim())) {
247 text.add(strings.get(i).substring(tag.length() + 4));
248 }
249 else {
250 text.add(strings.get(i).substring(2));
251 }
252 }
253 }
254 }
255
256 /***
257 * Description of the Method
258 *
259 *@return Description of the Return Value
260 */
261 public final boolean replyExists() {
262 return (numericCode > 0) || (text.size() > 0);
263 }
264
265 /***
266 * DOCUMENT ME!
267 *
268 * @return DOCUMENT ME!
269 */
270 public String toString() {
271 return generateReply();
272 }
273 }
This page was automatically generated by Maven